/* 
Set directory 
*/

    global root ENTER DIRECTORY

	global r4oc 		$root\Data\Young Lives\R4\UKDA-7931-stata11\stata11\india_r4\oc_hh_anon
	global r4yc 		$root\Data\Young Lives\R4\UKDA-7931-stata11\stata11\india_r4\yc_hh_anon
	global r3oc 		$root\Data\Young Lives\R3\survey_data_r3\india_r3\olderchild
	global r3yc 		$root\Data\Young Lives\R3\survey_data_r3\india_r3\youngerchild
	global r2oc 		$root\Data\Young Lives\R2\survey_data_r2\india_r2\Older Child
	global r2yc 		$root\Data\Young Lives\R2\survey_data_r2\india_r2\Younger Child

	global data 		$root\Dta\Young Lives\


use "$r2oc\inchildlevel12yrold.dta", clear
	
keep CHILDID EATV01 EATV02 EATV03 EATV04 EATV05 EATV06 EATV07 EATV08 EATV09 EATV10 EATV11 EATV12 EATV13 EATV14 EATV15 EATV16 EATV17 EATV18 EATV19 EATV20 ///
OWNV01 OWNV02 OWNV03 OWNV04 OWNV05 OWNV06 OWNV07 OWNV08 OWNV09 OWNV10 OWNV11 OWNV12 OWNV13 OWNV14 OWNV15 OWNV16 OWNV17 OWNV18 OWNV19 OWNV20

save "$data\r2oc_food.dta", replace

use "$r2yc\inchildlevel5yrold.dta", clear

keep CHILDID EATV01 EATV02 EATV03 EATV04 EATV05 EATV06 EATV07 EATV08 EATV09 EATV10 EATV11 EATV12 EATV13 EATV14 EATV15 EATV16 EATV17 EATV18 EATV19 EATV20 ///
OWNV01 OWNV02 OWNV03 OWNV04 OWNV05 OWNV06 OWNV07 OWNV08 OWNV09 OWNV10 OWNV11 OWNV12 OWNV13 OWNV14 OWNV15 OWNV16 OWNV17 OWNV18 OWNV19 OWNV20



append using "$data\r2oc_food.dta"

gen round = 2

foreach var of varlist EATV01 EATV02 EATV03 EATV04 EATV05 EATV06 EATV07 EATV08 EATV09 EATV10 EATV11 EATV12 EATV13 EATV14 EATV15 EATV16 EATV17 EATV18 EATV19 EATV20 ///
OWNV01 OWNV02 OWNV03 OWNV04 OWNV05 OWNV06 OWNV07 OWNV08 OWNV09 OWNV10 OWNV11 OWNV12 OWNV13 OWNV14 OWNV15 OWNV16 OWNV17 OWNV18 OWNV19 OWNV20 {
replace `var' = 0 if `var' <0 | `var' == .
}


egen crops = rowtotal(EATV01 EATV02 EATV03)
egen animals = rowtotal(EATV05 EATV06 EATV07 EATV08 EATV09 EATV10)
egen vege_fruit = rowtotal(EATV04 EATV11 EATV12)

egen owncrops = rowtotal(OWNV01 OWNV02 OWNV03)
egen ownanimals = rowtotal(OWNV05 OWNV06 OWNV07 OWNV08 OWNV09 OWNV10)
egen ownvege_fruit = rowtotal(OWNV04 OWNV11 OWNV12)

keep CHILDID round crops animals vege_fruit owncrops ownanimals ownvege_fruit

save "$data\r2_food.dta", replace

*Round3

use "$r3oc\in_oc_householdlevel.dta", clear

keep CHILDID VLRPR300 VLRPR301 VLRPR302 VLRPR320 VLRPR303 VLRPR304 VLRPR305 VLRPR306 VLRPR307 VLRPR308 VLRPR309 VLRPR310 VLRPR311 VLRPR312 VLRPR313 VLRPR314 VLRPR315 VLRPR316 VLRPR317 VLRPR318 VLRPR319 VLRPR321 VLRPR324 ///
OWNVR300 OWNVR301 OWNVR302 OWNVR303 OWNVR304 OWNVR305 OWNVR306 OWNVR307 OWNVR308 OWNVR309 OWNVR310 OWNVR311 OWNVR312 OWNVR313 OWNVR314 OWNVR315 OWNVR316 OWNVR317

save "$data\r3oc_food.dta", replace

use "$r3yc\in_yc_householdlevel.dta", clear

keep CHILDID VLRPR300 VLRPR301 VLRPR302 VLRPR320 VLRPR303 VLRPR304 VLRPR305 VLRPR306 VLRPR307 VLRPR308 VLRPR309 VLRPR310 VLRPR311 VLRPR312 VLRPR313 VLRPR314 VLRPR315 VLRPR316 VLRPR317 VLRPR318 VLRPR319 VLRPR321 VLRPR324 ///
OWNVR300 OWNVR301 OWNVR302 OWNVR303 OWNVR304 OWNVR305 OWNVR306 OWNVR307 OWNVR308 OWNVR309 OWNVR310 OWNVR311 OWNVR312 OWNVR313 OWNVR314 OWNVR315 OWNVR316 OWNVR317

append using "$data\r3oc_food.dta"


foreach var of varlist VLRPR300 VLRPR301 VLRPR302 VLRPR320 VLRPR303 VLRPR304 VLRPR305 VLRPR306 VLRPR307 VLRPR308 VLRPR309 VLRPR310 VLRPR311 VLRPR312 VLRPR313 VLRPR314 VLRPR315 VLRPR316 VLRPR317 VLRPR318 VLRPR319 VLRPR321 VLRPR324 ///
OWNVR300 OWNVR301 OWNVR302 OWNVR303 OWNVR304 OWNVR305 OWNVR306 OWNVR307 OWNVR308 OWNVR309 OWNVR310 OWNVR311 OWNVR312 OWNVR313 OWNVR314 OWNVR315 OWNVR316 OWNVR317 {

replace `var' = 0 if `var' <0 | `var' == .
}

gen round = 3

egen crops = rowtotal(VLRPR301 VLRPR302 VLRPR320 VLRPR303)
egen animals = rowtotal(VLRPR305 VLRPR306 VLRPR307 VLRPR308 VLRPR309 VLRPR310)
egen vege_fruit = rowtotal(VLRPR304 VLRPR311 VLRPR312)

egen owncrops = rowtotal(OWNVR301 OWNVR302 OWNVR303 OWNVR304)
egen ownanimals = rowtotal(OWNVR306 OWNVR307 OWNVR308 OWNVR309 OWNVR310 OWNVR311)
egen ownvege_fruit = rowtotal(OWNVR305 OWNVR312 OWNVR313)

keep CHILDID round crops animals vege_fruit owncrops ownanimals ownvege_fruit

save "$data\r3_food.dta", replace



*Round4

use "$r4oc\in_r4_ochh_foodconsumption.dta", clear

decode FOODIDR4, gen(temp)

bys CHILDCODE: egen tcrops = sum(VLRPR4) if temp == "Pulses/Lentils/Beans" | temp == "Pasta/Rice" | temp == "Bread/Wheat flour (processed)" 
bys CHILDCODE: egen tanimals = sum(VLRPR4) if temp == "Meat Products (including chicken, excluding fish)" | temp == "Milk or milk products (milk, butter, cheese, yoghurt, etc.)" 
bys CHILDCODE: egen tvege_fruit = sum(VLRPR4) if temp == "Vegetables"

bys CHILDCODE: egen crops = max(tcrops)
bys CHILDCODE: egen animals = max(tanimals)
bys CHILDCODE: egen vege_fruit = max(tvege_fruit)

bys CHILDCODE: egen towncrops = sum(OWNVR4) if temp == "Pulses/Lentils/Beans" | temp == "Pasta/Rice" | temp == "Bread/Wheat flour (processed)" 
bys CHILDCODE: egen townanimals = sum(OWNVR4) if temp == "Meat Products (including chicken, excluding fish)" | temp == "Milk or milk products (milk, butter, cheese, yoghurt, etc.)" 
bys CHILDCODE: egen townvege_fruit = sum(OWNVR4) if temp == "Vegetables"

bys CHILDCODE: egen owncrops = max(towncrops)
bys CHILDCODE: egen ownanimals = max(townanimals)
bys CHILDCODE: egen ownvege_fruit = max(townvege_fruit)

bys CHILDCODE: keep if _n == 1

drop FOODIDR4 HHCONSR4 VLRPR4 OWNVR4 GFTVR4 GFSRR4 temp tcrops tanimals tvege_fruit towncrops townanimals townvege_fruit 


save "$data\r4oc_food.dta", replace

use "$r4yc\in_r4_ychh_foodconsumption.dta", clear

decode FOODIDR4, gen(temp)

bys CHILDCODE: egen tcrops = sum(VLRPR4) if temp == "Pulses/Lentils/Beans" | temp == "Pasta/Rice" | temp == "Bread/Wheat flour (processed)" | temp == "Cereals (barley, wheat, teff, sorghum, maize, etc.) (unprocessed)" 
bys CHILDCODE: egen tanimals = sum(VLRPR4) if temp == "Fresh fish" | temp == "Processed Fish (tinned)" | temp == "Milk or milk products (milk, butter, cheese, yoghurt, etc.)"  | temp == "Eggs" | temp == "Powdered/Formula Milk"  | temp == "Meat Products" 
bys CHILDCODE: egen tvege_fruit = sum(VLRPR4) if temp == "Vegetables" | temp == "Tubers/Potatoes/Root crops" | temp == "Fruit"


bys CHILDCODE: egen crops = max(tcrops)
bys CHILDCODE: egen animals = max(tanimals)
bys CHILDCODE: egen vege_fruit = max(tvege_fruit)

bys CHILDCODE: egen towncrops = sum(OWNVR4) if temp == "Pulses/Lentils/Beans" | temp == "Pasta/Rice" | temp == "Bread/Wheat flour (processed)" | temp == "Cereals (barley, wheat, teff, sorghum, maize, etc.) (unprocessed)" 
bys CHILDCODE: egen townanimals = sum(OWNVR4) if temp == "Fresh fish" | temp == "Processed Fish (tinned)" | temp == "Milk or milk products (milk, butter, cheese, yoghurt, etc.)"  | temp == "Eggs" | temp == "Powdered/Formula Milk"  | temp == "Meat Products" 
bys CHILDCODE: egen townvege_fruit = sum(OWNVR4) if temp == "Vegetables" | temp == "Tubers/Potatoes/Root crops" | temp == "Fruit"


bys CHILDCODE: egen owncrops = max(towncrops)
bys CHILDCODE: egen ownanimals = max(townanimals)
bys CHILDCODE: egen ownvege_fruit = max(townvege_fruit)

bys CHILDCODE: keep if _n == 1

drop FOODIDR4 HHCONSR4 VLRPR4 OWNVR4 GFTVR4 GFSRR4 temp tcrops tanimals tvege_fruit  towncrops townanimals townvege_fruit 

append using "$data\r4oc_food.dta"

gen round = 5

save "$data\r4_food.dta", replace

append using "$data\r3_food.dta"
append using "$data\r2_food.dta"

gen temp = 0
egen temp1 = concat(temp CHILDCODE) if CHILDCODE < 100000
tostring CHILDCODE, replace
replace temp1 = CHILDCODE if temp1 == ""
drop temp
gen temp = "IN"
egen temp2 = concat(temp temp1)
replace CHILDID = temp2 if round == 5
drop temp* CHILDCODE

foreach var of varlist crops animals vege_fruit owncrops ownanimals ownvege_fruit{
gen ln`var' = ln(`var')
drop `var'
replace ln`var' = 0 if ln`var' == .
}

ren CHILDID childid

save "$data\food.dta", replace
